// PolicyCompute_v1

export default function (cellData, step = 4) {
  for (let i = 0; i < cellData.length; i++) {
    let cell = cellData[i];
    let sectorRequired = 1;
    let sectorReason = "最低扇区数量";
    let matchedTags = [];
    for (let j = 0; j < computingOptions.length; j++) {
      let option = computingOptions[j];
      let valueCumulated = 0;
      for (let airport in cell.airport) {
        if (option.airport.includes(airport)) {
          for (let type in cell.airport[airport].type) {
            if (option.type.includes(type)) {
              let total = cell.airport[airport].type[type].total;

              // test for new params

              // valueCumulated += total * (type == "ARR" ? basicMultiplerArr : basicMultiplerDep);

              valueCumulated += total;
            }
          }
        }
      }

      // test for new params

      // valueCumulated =
      //   valueCumulated *
      //   (valueCumulated < optThresholdBottom
      //     ? optMultiplerBottom
      //     : valueCumulated > optThresholdTop
      //     ? optMultiplerTop
      //     : 1);

      // fix step
      valueCumulated = valueCumulated * (4 / step);
      for (let k = 0; k < option.threshold.length; k++) {
        let threshold = option.threshold[k];
        let tagsValid = true;
        if (threshold.requiredTags && threshold.requiredTags.length > 0) {
          // match Tags
          for (let t = 0; t < threshold.requiredTags.length; t++) {
            if (!matchedTags.includes(threshold.requiredTags[t])) {
              tagsValid = false;
              break;
            }
          }
        }
        if (
          tagsValid &&
          valueCumulated >= threshold.valueCumulated &&
          sectorRequired >= threshold.sectorAlready &&
          sectorRequired < threshold.sectorRequired
        ) {
          sectorRequired = threshold.sectorRequired;
          sectorReason = option.description;
          matchedTags.push(option.tag);
          // console.log(cell.moment, option.description, sectorRequired);
        }
      }
    }
    cellData[i]["sectorRequire"] = sectorRequired;
    cellData[i]["sectorReason"] = sectorReason;
  }
  return cellData;
}

// const basicMultiplerArr = 1.5;
// const basicMultiplerDep = 0.5;
// const optThresholdTop = 105;
// const optThresholdBottom = 90;
// const optMultiplerTop = 0.9;
// const optMultiplerBottom = 1.1;

const computingOptions = [
  {
    description:
      "总量 > 116@8扇; 105@7扇; 95@6扇; 75@5扇; 65@4扇; 45@3扇; 21@2扇",
    tag: "total_general_compute",
    airport: ["ZUUU", "ZUTF", "ZUMY"],
    type: ["ARR", "DEP"],
    threshold: [
      {
        valueCumulated: 116,
        sectorAlready: 0,
        sectorRequired: 8,
        requiredTags: [],
      },
      {
        valueCumulated: 105,
        sectorAlready: 0,
        sectorRequired: 7,
        requiredTags: [],
      },
      {
        valueCumulated: 94,
        sectorAlready: 0,
        sectorRequired: 6,
        requiredTags: [],
      },
      {
        valueCumulated: 80,
        sectorAlready: 0,
        sectorRequired: 5,
        requiredTags: [],
      },
      {
        valueCumulated: 65,
        sectorAlready: 0,
        sectorRequired: 4,
        requiredTags: [],
      },
      {
        valueCumulated: 45,
        sectorAlready: 0,
        sectorRequired: 3,
        requiredTags: [],
      },
      {
        valueCumulated: 21,
        sectorAlready: 0,
        sectorRequired: 2,
        requiredTags: [],
      },
      {
        valueCumulated: 0,
        sectorAlready: 0,
        sectorRequired: 1,
        requiredTags: [],
      },
    ],
  },
  {
    description: "总量@3扇; ZUUU进港?20架次; @4扇",
    tag: "ZUUU_ARR_20",
    airport: ["ZUUU"],
    type: ["ARR"],
    threshold: [
      {
        valueCumulated: 21,
        sectorAlready: 3,
        sectorRequired: 4,
        requiredTags: [],
      },
    ],
  },
  {
    description: "总量@3扇; ZUTF进港>20架次; @4扇",
    tag: "ZUTF_ARR_20",
    airport: ["ZUTF"],
    type: ["ARR"],
    threshold: [
      {
        valueCumulated: 21,
        sectorAlready: 3,
        sectorRequired: 4,
        requiredTags: [],
      },
    ],
  },
  {
    description: "ZUUU进港架次≥19; @3扇",
    tag: "ZUUU_ARR_19",
    airport: ["ZUUU"],
    type: ["ARR"],
    threshold: [
      {
        valueCumulated: 19,
        sectorAlready: 0,
        sectorRequired: 3,
        requiredTags: [],
      },
    ],
  },
  {
    description: "ZUTF进港架次≥19; @3扇",
    tag: "ZUTF_ARR_19",
    airport: ["ZUTF"],
    type: ["ARR"],
    threshold: [
      {
        valueCumulated: 19,
        sectorAlready: 0,
        sectorRequired: 3,
        requiredTags: [],
      },
    ],
  },
  {
    description: "ZUUU/ZUTF进港架次均≥19; @5扇",
    tag: "ZUUU&ZUTF_ARR_19",
    airport: ["ZUUU", "ZUTF"],
    type: ["ARR"],
    threshold: [
      {
        valueCumulated: 38,
        sectorAlready: 0,
        sectorRequired: 5,
        requiredTags: ["ZUUU_ARR_19", "ZUTF_ARR_19"],
      },
    ],
  },
];
